package com.minyifang.wcspf.common.utils;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.minyifang.wcspf.common.widgets.CircleImageView;


public class AnimUtils {

    private Context mContext;
    private ViewGroup root;//动画层
    private float scale = 0.5f; //相对于原动画的比例
    private int duration = 1000;

    private int[] startViewLocation = new int[2];
    private int[] endViewLocation = new int[2];
    private int[] moveViewLocation = new int[2];
    private int[] startViewSize = new int[2];
    private int[] endViewSize = new int[2];
    private int[] moveViewSize = new int[2];

    public AnimUtils(Context context) {
        mContext = context;
    }

    public void setImageSize(float scale) {
        this.scale = scale;
    }

    public void setImageSize(int width, int height) {
        moveViewSize[0] = width;
        moveViewSize[1] = height;
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }

    public void startAnim(View startView, View endView) {
        if (startView == null || endView == null) {
            ToastHelper.getInstance(mContext).showWarn("无法对空对象执行动画");
            return;
        }
        CircleImageView moveView = new CircleImageView(mContext);
        root = (ViewGroup) ((Activity) mContext).getWindow().getDecorView();
        root.addView(moveView);
        calculateLocation(moveView, startView, endView);
        moveView.setImageDrawable(((ImageView) startView).getDrawable());
        // 计算位移
        int endX = (endViewLocation[0] + (endViewSize[0] - moveViewSize[0]) / 2) - moveViewLocation[0];
        int endY = (endViewLocation[1] + (endViewSize[1] - moveViewSize[1]) / 2) - moveViewLocation[1];

        TranslateAnimation tax = new TranslateAnimation(0, endX, 0, 0);
        tax.setInterpolator(new LinearInterpolator());
        TranslateAnimation tay = new TranslateAnimation(0, 0, 0, endY);
        tay.setInterpolator(new AccelerateInterpolator());
        ScaleAnimation sa = new ScaleAnimation(1, 0.5f, 1, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        RotateAnimation ra = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        AnimationSet set = new AnimationSet(true);
        set.addAnimation(sa);
        set.addAnimation(ra);
        set.addAnimation(tay);
        set.addAnimation(tax);
        set.setFillAfter(true);
        set.setDuration(duration);
        moveView.startAnimation(set);
        set.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                moveView.setVisibility(View.GONE);
                new Handler().post(() -> root.removeView(moveView));
            }
        });
    }

    private void calculateLocation(ImageView moveView, View startView, View endView) {

        startView.getLocationInWindow(startViewLocation);
        startViewSize[0] = startView.getWidth();
        startViewSize[1] = startView.getHeight();

        endView.getLocationInWindow(endViewLocation);
        endViewSize[0] = endView.getWidth();
        endViewSize[1] = endView.getHeight();

        if (moveViewSize[0] == 0 || moveViewSize[1] == 0) {
            moveViewSize[0] = (int) (startViewSize[0] * scale);
            moveViewSize[1] = (int) (startViewSize[1] * scale);
        }
        moveViewLocation[0] = startViewLocation[0] + (startViewSize[0] - moveViewSize[0]) / 2;
        moveViewLocation[1] = startViewLocation[1] + (startViewSize[1] - moveViewSize[1]) / 2;

        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(moveViewSize[0], moveViewSize[1]);
        lp.leftMargin = moveViewLocation[0];
        lp.topMargin = moveViewLocation[1];
        moveView.setLayoutParams(lp);
    }

}
